package com.promis.widget.dialog;

import com.promis.util.ColorUtils;
import com.promis.widget.R;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;

public abstract class PopupWindow extends android.widget.PopupWindow {

	private View anchor;
	private int[] location = new int[2];
	private boolean clickOutSideClose = true;

	public PopupWindow(Activity activity, View anchor) {
		super(activity);
		this.anchor = anchor;
		setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
		setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
		setBackgroundDrawable(new ColorDrawable(ColorUtils.getColor(activity, R.color.bg_transparent)));
		this.setAnimationStyle(R.style.PopupWindowAnimation);
		setFocusable(true);
		setTouchable(true);
		setOutsideTouchable(true);
		setContentView(onCreateView());

		getContentView().setFocusableInTouchMode(true);
		getContentView().setFocusable(true);
		getContentView().setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (clickOutSideClose) {
					clickOutSideClose = false;
					dismiss();
				}
			}
		});
		
		anchor.getLocationOnScreen(location);

		update();
	}
	
	public void showOnBottom() {
		showAsDropDown(anchor);
	}
	
	public void showOnTop() {
		showAtLocation(anchor, Gravity.NO_GRAVITY, location[0], location[1] - getHeight());
	}
	
	public void showOnLeft() {
		showAtLocation(anchor, Gravity.NO_GRAVITY, location[0] - getWidth(), location[1]);
	}
	
	public void showOnRight() {
		showAtLocation(anchor, Gravity.NO_GRAVITY, location[0] + anchor.getWidth(), location[1]);
	}
	
	protected abstract View onCreateView();
}
